package cn.com.czpb.client.module.news.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MyActivitymanager;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.news.MediaNewsBean;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.module.dialog.ShareDialog;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.StatUtils;
import cn.com.czpb.client.utils.StringUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 猪猪侠 on 2017/7/3.
 */

public class NewsItemListViewAdapterNew extends BaseMultiItemQuickAdapter<NewsBean, BaseViewHolder>{
    private boolean flag = true;
    private SPUtil spu;
    private String type;
    private Activity activity;
    /**
     * @param data 列表数据
     * @param flag 是否显示不感兴趣
     */
    public NewsItemListViewAdapterNew(Activity activity,List<NewsBean> data, boolean flag) {
        super(data);
        this.activity = activity;
        this.flag = flag;
        spu = SPUtil.getInstance();
        this.type = spu.getShowImage();
        addItemType(NewsBean.THREEPIC, R.layout.news_list_bigitem_layout);
        addItemType(NewsBean.LEFTPIC, R.layout.news_list_bigitem_layout);
        addItemType(NewsBean.NOPIC, R.layout.news_list_item_nopic_layout);
        addItemType(NewsBean.BIGPIC, R.layout.news_list_bigitem_layout);
        addItemType(NewsBean.SCROLLNEWS,R.layout.news_list_scroll_layout);
        addItemType(NewsBean.SCROLLSUBSCRIBE,R.layout.news_subscribe_list_scroll_layout);
        addItemType(NewsBean.VIDEOBIG, R.layout.news_list_videobig_layout);
    }
    @Override
    protected void convert(final BaseViewHolder helper, final NewsBean item) {
        switch (helper.getItemViewType()) {
            case NewsBean.THREEPIC://三联图
                helper.addOnClickListener(R.id.newsItem_root);
                helper.setText(R.id.newsitem_title, item.getTitle());//title
                helper.setText(R.id.newsitem_copyfrom, item.getCopyfrom());
                helper.setText(R.id.comment_num_id, item.getComcount());//评论数量
                helper.setText(R.id.praise_num_id,item.getPraisecount());
                helper.setText(R.id.tv_time_id, item.getUpdateTime());//时间
                List<String> imgs = item.getImgs();
                if (type.equals("0")) {
                    if (imgs != null && imgs.size() > 0) {
                        Glide.with(mContext).load(imgs.get(0)).crossFade().placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.news_3_item1));
//                        if (imgs.size() > 1) {
//                            Glide.with(mContext).load(imgs.get(1)).crossFade().placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.news_3_item2));
//                            if (imgs.size() > 2) {
//                                Glide.with(mContext).load(imgs.get(2)).crossFade().placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.news_3_item3));
//                            }
//                        }
                    }
                }
                if (!StringUtils.isEmpty(item.getNewsflag())){
                    helper.setVisible(R.id.img_newstype_id,true);
                    helper.setText(R.id.img_newstype_id, item.getNewsflag());//title
                }else{
                    helper.setVisible(R.id.img_newstype_id,false);
                }

                break;
            case NewsBean.LEFTPIC:
                helper.addOnClickListener(R.id.newsItem_root);
                helper.setText(R.id.newsitem_title, item.getTitle());//title
                helper.setText(R.id.newsitem_copyfrom, item.getCopyfrom());//copyfrom
                helper.setText(R.id.comment_num_id, item.getComcount());//跟帖数
                helper.setText(R.id.praise_num_id,item.getPraisecount());//点赞数
                helper.setText(R.id.tv_time_id, item.getUpdateTime());//时间
//                Glide.with(mContext).load(item.getNewsflag()).crossFade().into((ImageView) helper.getView(R.id.img_newstype_id));
                //视频 展示视频标签
                if ("6".equals(item.getRtype())) {
                    helper.setVisible(R.id.img_play_id, true);
                } else {
                    helper.setVisible(R.id.img_play_id, false);
                }
                if (type.equals("0")) {
                    if (helper.getView(R.id.newsitem_img).getVisibility() == View.VISIBLE
                            && null != item.getImgs()
                            && item.getImgs().size() > 0) {
                        Glide.with(mContext).load(item.getImgs().get(0)).crossFade()
                                .placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.newsitem_img));
                    } else {
                        Glide.with(mContext).load("").crossFade()
                                .placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.newsitem_img));
                    }
                }
                if (!StringUtils.isEmpty(item.getNewsflag())){
                    helper.setVisible(R.id.img_newstype_id,true);
                    helper.setText(R.id.img_newstype_id, item.getNewsflag());//title
                }else{
                    helper.setVisible(R.id.img_newstype_id,false);
                }
                break;

            case NewsBean.NOPIC:
                helper.addOnClickListener(R.id.newsItem_root);
                helper.setText(R.id.newsitem_title, item.getTitle());//title
                helper.setText(R.id.newsitem_copyfrom, item.getCopyfrom());
                helper.setText(R.id.comment_num_id, item.getComcount());//评论数量
                helper.setText(R.id.praise_num_id,item.getPraisecount());//点赞数
                helper.setText(R.id.tv_time_id, item.getUpdateTime());//时间
                if (!StringUtils.isEmpty(item.getNewsflag())){
                    helper.setVisible(R.id.img_newstype_id,true);
                    helper.setText(R.id.img_newstype_id, item.getNewsflag());//title
                }else{
                    helper.setVisible(R.id.img_newstype_id,false);
                }
                break;
            case NewsBean.BIGPIC:
                helper.addOnClickListener(R.id.newsItem_root);
                helper.setText(R.id.newsitem_title, item.getTitle());//title
                helper.setText(R.id.newsitem_copyfrom, item.getCopyfrom());//copyfrom
                helper.setText(R.id.comment_num_id, item.getComcount());//跟帖数
                helper.setText(R.id.praise_num_id,item.getPraisecount());//点赞数
                helper.setText(R.id.tv_time_id, item.getUpdateTime());//时间
                //视频 展示视频标签
                if ("6".equals(item.getRtype())) {
                    helper.setVisible(R.id.img_play_id, true);
                } else {
                    helper.setVisible(R.id.img_play_id, false);
                }
                if (type.equals("0")) {
                    Glide.with(mContext).load(item.getImgs().get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.newsitem_img));
                }
                if (!StringUtils.isEmpty(item.getNewsflag())){
                    helper.setVisible(R.id.img_newstype_id,true);
                    helper.setText(R.id.img_newstype_id, item.getNewsflag());//title
                }else{
                    helper.setVisible(R.id.img_newstype_id,false);
                }
                break;
            case NewsBean.SCROLLNEWS:
                if(item.getMedianews() != null){
                    LogUtils.i("medianews.size:"+item.getMedianews().size()+"");
                    final HorizontalListViewAdapter hadapter = new HorizontalListViewAdapter(mContext,item.getMedianews());
                    helper.setOnClickListener(R.id.scroll_nickname_rootll, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PageCtrl.start2SubscribeDetailActivity(activity,item.getMedianews().get(0).getWriterinfo().getUid(),item.getMedianews().get(0).getWriterinfo().getTid());
                        }
                    });
                    helper.setAdapter(R.id.news_hlistview,hadapter);
                    if(item.getMedianews().get(0).getWriterinfo().getIsfocus().equals("1")){
                        helper.setVisible(R.id.img_subscribe_scroll_news,false);
                        helper.setVisible(R.id.img_removesubscribe_scroll_news,true);
                    }else{
                        helper.setVisible(R.id.img_subscribe_scroll_news,true);
                        helper.setVisible(R.id.img_removesubscribe_scroll_news,false);
                    }
                    helper.setText(R.id.tv_subscribe_scroll_news,item.getMedianews().get(0).getWriterinfo().getNickname());
                    helper.setOnClickListener(R.id.img_subscribe_scroll_news, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogUtils.i("订阅！");
                            helper.setVisible(R.id.img_removesubscribe_scroll_news,true);
                            helper.setVisible(R.id.img_subscribe_scroll_news,false);
                            focusUser(item.getMedianews().get(0).getWriterinfo().getUid());
                        }
                    });
                    helper.setOnClickListener(R.id.iv_scroll_news_share, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(spu.getUser() != null) {
                                String imgurl = null;
                                if (null != item.getImgs() && item.getImgs().size() > 0) {
                                    imgurl = item.getImgs().get(0);
                                }else{
                                    imgurl = item.getMedianews().get(0).getImgs().get(0);
                                }
                                String sharelink = item.getNewsurl();
                                ShareDialog dialog = new ShareDialog(activity, R.style.Sharedialog, "广西云订阅号："+item.getTitle(), sharelink, imgurl, 1);
                                dialog.show();
                            }else{
                                PageCtrl.start2LoginActivity(activity);
                            }
                        }
                    });
                    helper.setOnClickListener(R.id.img_removesubscribe_scroll_news, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogUtils.i("取消订阅！");
                            helper.setVisible(R.id.img_removesubscribe_scroll_news,false);
                            helper.setVisible(R.id.img_subscribe_scroll_news,true);
                            removeFocusUser(item.getMedianews().get(0).getWriterinfo().getUid());
                        }
                    });
                    helper.setOnItemClickListener(R.id.news_hlistview, new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            MediaNewsBean mediaNewsBean = (MediaNewsBean) hadapter.getItem(position);
                            NewsBean newsBean = new NewsBean(mediaNewsBean);
                            switch (view.getId()) {
                                case R.id.scroll_news_rootll:
                                    ////REFACTOR: 2017/8/18 添加浏览量bug
                                    StatUtils.addCount(newsBean);
                                    MyActivitymanager.pushActivity(activity, newsBean, newsBean.getTid(), "newsitem", newsBean.getRvalue());
                                    LogUtils.e("rtype--" + newsBean.getRtype());
                                    break;
                            }
                        }
                    });
                }
//                LinearLayoutManager ms= new LinearLayoutManager(App.getContext());
//                ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
//                HScrollNewsItemAdapter scrolladapter = new HScrollNewsItemAdapter(item.getMedianews(),true);
//                RecyclerView myrecycleview = helper.getView(R.id.recycler_gxhdy_id);
//                myrecycleview.setLayoutManager(ms);
//                myrecycleview.setAdapter(scrolladapter);
                break;
            case NewsBean.SCROLLSUBSCRIBE:
                if(item.getMedianews() != null){
                    LogUtils.i("medianews.size:"+item.getMedianews().size()+"");
                    final HListViewAdapterForSubscribe hadapter = new HListViewAdapterForSubscribe(mContext,item.getMedianews());
                    helper.setOnClickListener(R.id.scroll_nickname_rootll, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PageCtrl.start2SubscribeDetailActivity(activity,item.getMedianews().get(0).getWriterinfo().getUid(),item.getMedianews().get(0).getWriterinfo().getTid());
                        }
                    });
                    helper.setAdapter(R.id.news_hlistview,hadapter);
//                    if(item.getMedianews().get(0).getWriterinfo().getIsfocus().equals("1")){
//                        helper.setVisible(R.id.img_subscribe_scroll_news,false);
//                        helper.setVisible(R.id.img_removesubscribe_scroll_news,true);
//                    }else{
//                        helper.setVisible(R.id.img_subscribe_scroll_news,true);
//                        helper.setVisible(R.id.img_removesubscribe_scroll_news,false);
//                    }
                    helper.setText(R.id.tv_subscribe_scroll_news,item.getMedianews().get(0).getWriterinfo().getNickname());
//                    helper.setOnClickListener(R.id.img_subscribe_scroll_news, new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            LogUtils.i("订阅！");
//                            helper.setVisible(R.id.img_removesubscribe_scroll_news,true);
//                            helper.setVisible(R.id.img_subscribe_scroll_news,false);
//                            focusUser(item.getMedianews().get(0).getWriterinfo().getUid());
//                        }
//                    });
//                    helper.setOnClickListener(R.id.iv_scroll_news_share, new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//
//                        }
//                    });
//                    helper.setOnClickListener(R.id.img_removesubscribe_scroll_news, new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            LogUtils.i("取消订阅！");
//                            helper.setVisible(R.id.img_removesubscribe_scroll_news,false);
//                            helper.setVisible(R.id.img_subscribe_scroll_news,true);
//                            removeFocusUser(item.getMedianews().get(0).getWriterinfo().getUid());
//                        }
//                    });
                    helper.setOnItemClickListener(R.id.news_hlistview, new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            MediaNewsBean mediaNewsBean = (MediaNewsBean) hadapter.getItem(position);
                            NewsBean newsBean = new NewsBean(mediaNewsBean);
                            switch (view.getId()) {
                                case R.id.scroll_news_rootll:
                                    ////REFACTOR: 2017/8/18 添加浏览量bug
                                    StatUtils.addCount(newsBean);
                                    MyActivitymanager.pushActivity(activity, newsBean, newsBean.getTid(), "newsitem", newsBean.getRvalue());
                                    LogUtils.e("rtype--" + newsBean.getRtype());
                                    break;
                            }
                        }
                    });
                }
//                LinearLayoutManager ms= new LinearLayoutManager(App.getContext());
//                ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
//                HScrollNewsItemAdapter scrolladapter = new HScrollNewsItemAdapter(item.getMedianews(),true);
//                RecyclerView myrecycleview = helper.getView(R.id.recycler_gxhdy_id);
//                myrecycleview.setLayoutManager(ms);
//                myrecycleview.setAdapter(scrolladapter);
                break;
            case NewsBean.VIDEOBIG:
                helper.addOnClickListener(R.id.newsItem_root);
                helper.setText(R.id.newsitem_title, item.getTitle());//title
                helper.setText(R.id.newsitem_copyfrom, item.getCopyfrom());//copyfrom
                helper.setText(R.id.comment_num_id, item.getComcount());//跟帖数
                helper.setText(R.id.praise_num_id,item.getPraisecount());//点赞数
                helper.setText(R.id.tv_time_id, item.getUpdateTime());//时间
                helper.addOnClickListener(R.id.rl_root_id);
                //视频 展示视频标签
//                if ("6".equals(item.getRtype())) {
//                    helper.setVisible(R.id.img_play_id, true);
//                } else {
//                    helper.setVisible(R.id.img_play_id, false);
//                }
                if (type.equals("0")) {
                    Glide.with(mContext).load(item.getImgs().get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into((ImageView) helper.getView(R.id.newsitem_img));
                }
                if (!StringUtils.isEmpty(item.getNewsflag())){
                    helper.setVisible(R.id.img_newstype_id,true);
                    helper.setText(R.id.img_newstype_id, item.getNewsflag());//title
                }else{
                    helper.setVisible(R.id.img_newstype_id,false);
                }
                break;
        }
    }


    //订阅达人号
    private void focusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        Factory.provideHttpService().focusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    //取消订阅
    private void removeFocusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        Factory.provideHttpService().removeFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

}
